package com.psfd.demo.controller;

import com.psfd.demo.bean.Singer;
import com.psfd.demo.service.SingerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
public class SingerController {

    @Autowired
    private SingerService singerService;

    @RequestMapping("/singer")
    public ModelAndView query(){
        List<Singer> list = singerService.list();
        for (Singer singer : list) {
            System.out.println(singer);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("singerList",singerService.list());
        modelAndView.setViewName("singerlist");
        return modelAndView;
    }

    @RequestMapping("/addsinger")
    public ModelAndView add(Singer singer){
        singerService.save(singer);
        return query();
    }

    @RequestMapping("/updatesingerhtml")
    public ModelAndView updatehtml(Integer siId){
        System.out.println(siId);
        ModelAndView view = new ModelAndView();
        view.addObject("siId",siId);
        view.setViewName("singerupdate");
        return view;
    }

    @RequestMapping("/updatesinger")
    public ModelAndView update(Singer singer){
        System.out.println(singer.getSingerId());
        singerService.updateById(singer);
        return query();
    }

    @RequestMapping("/deletesinger")
    public ModelAndView delete(Integer id){
        singerService.removeById(id);
        return query();
    }
}
